/**
 * 
 */
package be.ipl.pigolo.servlets;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import be.ipl.pigolo.exceptions.ArgumentInvalideException;
import be.ipl.pigolo.usecases.GestionPartie;
import be.ipl.pigolo.util.Util;

/**
 * Servlet implementation class gestion de l'index du site : connexion,
 * déconnexion, erreurs de login
 * 
 * @author lspitaels,sdoublier
 */
@WebServlet(name = "accueil.html")
public class Accueil extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@EJB
	GestionPartie gestPartie;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String erreur = (String) request.getParameter("erreurSession");

		if (Util.checkNullEmpty(erreur)) {
			doPost(request, response);
		} else {
			request.setAttribute("erreur", erreur);
			getServletContext().getNamedDispatcher("index.html").forward(
					request, response);
			return;
		}
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("typeDAction");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		HttpSession session = request.getSession(true);

		if (Util.checkNullEmpty(username) || Util.checkNullEmpty(password)) {
			request.setAttribute("erreur", "Veuillez remplir tout les champs");
			getServletContext().getNamedDispatcher("index.html").forward(
					request, response);
			return;
		} else {
			if (action.equals("connexion")) {
				try {
					if (!gestPartie.connexion(username, password)) {
						throw new ArgumentInvalideException();
					}
				} catch (ArgumentInvalideException e) {
					request.setAttribute("erreur", "Problème de login");
					getServletContext().getNamedDispatcher("index.html")
							.forward(request, response);
					return;
				} catch (Exception e) {
					request.setAttribute("erreur", "Fermier inexistant");
					getServletContext().getNamedDispatcher("index.html")
							.forward(request, response);
					return;
				}
			} else {
				try {
					if (!gestPartie.inscrire(username, password)) {
						throw new ArgumentInvalideException();
					}
					if (!gestPartie.connexion(username, password)) {
						throw new ArgumentInvalideException();
					}
				} catch (ArgumentInvalideException e) {
					request.setAttribute("erreur", "Problème à l'inscription");
					getServletContext().getNamedDispatcher("index.html")
							.forward(request, response);
					return;
				}
			}

			synchronized (session) {
				session.setAttribute("fermier", username);
				session.setAttribute("gestionpartie", gestPartie);
			}

			response.sendRedirect(response
					.encodeRedirectURL("listedesparties.html"));
		}
	}
}
